package service

/**
 * @title 用户服务
 *
 * @author wz
 *
 * @date 2025/10/23 14:13
 *
 * @version 1.0
 */

import (
	"gorm.io/gorm"
	"test-gin/internal/model"
)

type UserService struct {
	db *gorm.DB
}

func NewUserService(db *gorm.DB) *UserService {
	return &UserService{db: db}
}

// CreateUser 创建用户
func (s *UserService) CreateUser(user *model.User) error {
	return s.db.Create(user).Error
}

// GetUserById 通过Id获取用户
func (s *UserService) GetUserById(id uint) (*model.User, error) {
	var user model.User
	err := s.db.First(&user, id).Error
	return &user, err
}

// UpdateUser 更新用户信息
func (s *UserService) UpdateUser(user *model.User) error {
	return s.db.Model(&user).Updates(user).Error
}

// DeleteUser 删除用户
func (s *UserService) DeleteUser(id uint) error {
	return s.db.Delete(&model.User{}, id).Error
}
